package itemCalc.common;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

public class ContainerItemCalculator extends Container {

	private World worldObj;
	private int posX;
	private int posY;
	private int posZ;
	public int stackSize = 1;
	public int index;
	public List finalList = new ArrayList();
	public ContainerItemCalculator(InventoryPlayer par1InventoryPlayer, World par2World, int par3, int par4, int par5)
	{
		this.worldObj = par2World;
		this.posX = par3;
		this.posY = par4;
		this.posZ = par5;
		int l;
		int i1;

		for (l = 0; l < 3; ++l){
			for (i1 = 0; i1 < 9; ++i1){
				this.addSlotToContainer(new Slot(par1InventoryPlayer, i1 + l * 9 + 9, 8 + i1 * 18, 84 + l * 18));
			}
		}

		for (l = 0; l < 9; ++l){
			this.addSlotToContainer(new Slot(par1InventoryPlayer, l, 8 + l * 18, 142));
		}


	}


	public void onCraftMatrixChanged(IInventory inSlot)
	{
		super.onCraftMatrixChanged(inSlot);

	}


	public void onContainerClosed(EntityPlayer par1EntityPlayer)
	{
		
	}


	@Override
	public boolean canInteractWith(EntityPlayer entityplayer) {
		return true;
	}


	public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)
    {
        ItemStack itemstack = null;
        Slot slot = (Slot)this.inventorySlots.get(par2);

        if (slot != null && slot.getHasStack())
        {
            ItemStack itemstack1 = slot.getStack();
            itemstack = itemstack1.copy();

            if (par2 == 0)
            {
                if (!this.mergeItemStack(itemstack1, 11, 47, true))
                {
                    return null;
                }

                slot.onSlotChange(itemstack1, itemstack);
            }
            else if (par2 >= 11 && par2 < 38)
            {
                if (!this.mergeItemStack(itemstack1, 38, 47, false))
                {
                    return null;
                }
            }
            else if (par2 >= 38 && par2 < 47)
            {
                if (!this.mergeItemStack(itemstack1, 11, 38, false))
                {
                    return null;
                }
            }
            else if (!this.mergeItemStack(itemstack1, 11, 47, false))
            {
                return null;
            }

            if (itemstack1.stackSize == 0)
            {
                slot.putStack((ItemStack)null);
            }
            else
            {
                slot.onSlotChanged();
            }

            if (itemstack1.stackSize == itemstack.stackSize)
            {
                return null;
            }

            slot.onPickupFromSlot(par1EntityPlayer, itemstack1);
        }

        return itemstack;
    }


	public int getCurrentRecipeIndex() {
		return this.index;
	}

}
